
package client_package;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.TreeMap;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class VueInscription extends VueAvecBar {
	private static final long serialVersionUID = 1L;

	protected JPanel contentPane; 		//protégé pour permetre a vueModificationInfo de se rajouter des elements
	
	private JPasswordField passwordField;
	private JTextField textFieldLogin;
	private JTextField textFieldnom;
	
	private ButtonGroup buttonGroup;
	private JRadioButton rdbtnHomme;
	private JRadioButton rdbtnFemme;
	
	private ButtonGroup buttonGroup1;
	private JRadioButton rdbtnLocuteur;
	private JRadioButton rdbtnInterprete;
	
	private JList listLangueMaternelle;
	private JList listLanguesParlees;
	
	private DefaultListModel modelLangues;
	private DefaultListModel model;
	
	private TreeMap<String,String> langues;
	
	
	private TreeMap<String,String> languesSelectionne;
	
	private JLabel lblLanguesParles;
	private boolean firstClic;
	private String data;
	
	
	public String getData() {
		return data;
	}
	
	public void setRdbtnLocuteur(JRadioButton rdbtnLocuteur) {
		this.rdbtnLocuteur = rdbtnLocuteur;
	}

	public void setRdbtnInterprete(JRadioButton rdbtnInterprete) {
		this.rdbtnInterprete = rdbtnInterprete;
	}

	public JRadioButton getRdbtnLocuteur() {
		return rdbtnLocuteur;
	}

	public JRadioButton getRdbtnInterprete() {
		return rdbtnInterprete;
	}

	public TreeMap<String, String> getLangues() {
		return langues;
	}

	public void setLangues(TreeMap<String, String> langues) {
		this.langues = langues;
	}

	public void setModel(DefaultListModel model) {
		this.model = model;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public JButton getBtnAnnuler() {
		return btnAnnuler;
	}

	public JTextField getTextFieldLogin() {
		return textFieldLogin;
	}

	public JTextField getTextFieldnom() {
		return textFieldnom;
	}

	public JTextField getTextFieldprenom() {
		return textFieldprenom;
	}

	public JScrollPane getScrollPaneLangueMaternelle() {
		return scrollPaneLangueMaternelle;
	}

	public JLabel getLblLanguesParles() {
		return lblLanguesParles;
	}

	public JScrollPane getScrollPaneLanguesParlees() {
		return scrollPaneLanguesParlees;
	}

	public JRadioButton getRdbtnHomme() {
		return rdbtnHomme;
	}

	public JRadioButton getRdbtnFemme() {
		return rdbtnFemme;
	}

	public JList getListLangueMaternelle() {
		return listLangueMaternelle;
	}

	public JList getListLanguesParlees() {
		return listLanguesParlees;
	}

	public DefaultListModel getModelLangues() {
		return modelLangues;
	}

	public DefaultListModel getModel() {
		return model;
	}

	public TreeMap<String, String> getLanguesSelectionne() {
		return languesSelectionne;
	}

	private JTextField textFieldprenom;
	
	private JButton btnInscription;
	public JButton getBtnInscription() {
		return btnInscription;
	}

	private JButton btnAnnuler;
	
	private JScrollPane scrollPaneLangueMaternelle;
	private JScrollPane scrollPaneLanguesParlees;
	
	

	public VueInscription(Controleur c) {
		super(c);
		firstClic = true;
		data = null;
		setResizable(false);
		setBounds(100, 100, 438, 637);
		setTitle("Enregistrement");
		langues = getControleur().getLangues();
		buttonGroup = new ButtonGroup();
		buttonGroup1 = new ButtonGroup();
		languesSelectionne = new TreeMap<String, String>();
		this.addWindowListener (new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
				getControleur().fermerVue(VueInscription.this);
				getControleur().getOut().println("exit");
				getControleur().getOut().flush();
			}
		});		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textFieldLogin = new JTextField();
		textFieldLogin.setBounds(200, 17, 114, 19);
		contentPane.add(textFieldLogin);
		textFieldLogin.setColumns(10);
		
		JLabel lblLogin = new JLabel("Login : ");
		lblLogin.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLogin.setBounds(0, 19, 200, 15);
		contentPane.add(lblLogin);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(200, 58, 114, 19);
		contentPane.add(passwordField);
		
		JLabel lblMotDePasse = new JLabel("Mot de passe : ");
		lblMotDePasse.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMotDePasse.setBounds(0, 58, 200, 15);
		contentPane.add(lblMotDePasse);
		
		
		
		JLabel lblNom = new JLabel("Nom : ");
		lblNom.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNom.setBounds(0, 99, 200, 15);
		contentPane.add(lblNom);
		
		JLabel lblPrenom = new JLabel("Prénom : ");
		lblPrenom.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPrenom.setBounds(0, 135, 200, 15);
		contentPane.add(lblPrenom);
		
		JLabel lblSexe = new JLabel("Sexe : ");
		lblSexe.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSexe.setBounds(0, 172, 200, 15);
		contentPane.add(lblSexe);
		
		textFieldnom = new JTextField();
		textFieldnom.setBounds(200, 99, 114, 19);
		contentPane.add(textFieldnom);
		textFieldnom.setColumns(10);
		
		textFieldprenom = new JTextField();
		textFieldprenom.setBounds(200, 135, 114, 19);
		contentPane.add(textFieldprenom);
		textFieldprenom.setColumns(10);
		
		
		
		scrollPaneLangueMaternelle = new JScrollPane();
		scrollPaneLangueMaternelle.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneLangueMaternelle.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPaneLangueMaternelle.setBounds(200, 302, 168, 98);
		contentPane.add(scrollPaneLangueMaternelle);
		
		model = new DefaultListModel();
		for(String s : langues.values()) {
			model.addElement(s);	
		}
		

		listLangueMaternelle = new JList(model);
		listLangueMaternelle.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				listLanguesParlees.clearSelection();
				if (!listLangueMaternelle.isSelectionEmpty() && rdbtnInterprete.isSelected()) {
					String LangueMat = (String) listLangueMaternelle.getSelectedValue();

					if(firstClic) {
						data = LangueMat;
						firstClic = false;
					} else {
						int i = 0;
						while (i < modelLangues.size() && (((String)modelLangues.get(i)).compareToIgnoreCase(data) < 0)) {
							i++;
						}
						modelLangues.add(i, data);
						data = LangueMat;
					}
					
					modelLangues.removeElement(LangueMat);
					listLanguesParlees.setEnabled(true);
				} else {
					listLanguesParlees.setEnabled(false);
				}
			}
		});
		listLangueMaternelle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPaneLangueMaternelle.setViewportView(listLangueMaternelle);
		listLangueMaternelle.getSelectedValue();
		
		
		scrollPaneLanguesParlees = new JScrollPane();
		scrollPaneLanguesParlees.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneLanguesParlees.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPaneLanguesParlees.setBounds(200, 412, 168, 98);
		contentPane.add(scrollPaneLanguesParlees);
		
		
		modelLangues = new DefaultListModel();
		for(String s : langues.values()) {
			modelLangues.addElement(s);	
		}
		listLanguesParlees = new JList(modelLangues);
		listLanguesParlees.setEnabled(false);
		scrollPaneLanguesParlees.setViewportView(listLanguesParlees);
		
		
		JLabel lblNewLabel = new JLabel("Langue maternelle : ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(0, 304, 200, 15);
		contentPane.add(lblNewLabel);
		
		lblLanguesParles = new JLabel("Langue(s) parlée(s) : ");
		lblLanguesParles.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLanguesParles.setBounds(0, 414, 200, 15);
		contentPane.add(lblLanguesParles);
		
		rdbtnHomme = new JRadioButton("Homme");
		rdbtnHomme.setSelected(true);
		buttonGroup.add(rdbtnHomme);
		rdbtnHomme.setBounds(200, 168, 134, 23);
		contentPane.add(rdbtnHomme);
		
		rdbtnFemme = new JRadioButton("Femme");
		buttonGroup.add(rdbtnFemme);
		rdbtnFemme.setBounds(200, 196, 134, 23);
		contentPane.add(rdbtnFemme);
		
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueInscription.this);
				getControleur().getOut().println("exit");
				getControleur().getOut().flush();
			}
		});
		btnAnnuler.setBounds(66, 544, 107, 25);
		contentPane.add(btnAnnuler);
		

		btnInscription = new JButton("S'enregistrer");
		btnInscription.setBounds(236, 544, 132, 25);
		btnInscription.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String login = textFieldLogin.getText();
				char[] mdp = passwordField.getPassword();
				String nom = textFieldnom.getText();
				String prenom = textFieldprenom.getText();
				int[] languePar=null;
				String sexe = null;
				if(rdbtnHomme.isSelected()) {
					sexe = "Homme";
				} else {
					sexe = "Femme";
				}
				
				String type = null;
				if(rdbtnLocuteur.isSelected()) {
					type = "Locuteur";
				} else {
					type = "Interprete";
				}
				
				String LangueMat = (String) listLangueMaternelle.getSelectedValue();
				
				if (type.equals("Interprete"))
				{
					languePar = listLanguesParlees.getSelectedIndices();			
					for (int i=0;i < languePar.length ;i++)
					{ 
						String valeurLangue= (String) modelLangues.getElementAt(languePar[i]);
						languesSelectionne.put(valeurLangue, valeurLangue);
					}
				}
				getControleur().inscrire(login, mdp,nom,prenom,sexe,type,LangueMat,languesSelectionne);
			}
			});
		contentPane.add(btnInscription);
		
		JLabel lblNewLabel_1 = new JLabel("Type de Compte : ");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(0, 248, 200, 15);
		contentPane.add(lblNewLabel_1);
		
		
		
		rdbtnLocuteur = new JRadioButton("Locuteur");
		rdbtnLocuteur.setSelected(true);
		buttonGroup1.add(rdbtnLocuteur);
		rdbtnLocuteur.setBounds(200, 242, 134, 23);
		contentPane.add(rdbtnLocuteur);
		rdbtnLocuteur.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listLanguesParlees.setEnabled(false);
				listLanguesParlees.clearSelection();
				firstClic = true;
				int i = 0;
				try
				{
					while (i < getModelLangues().size() && (((String)getModelLangues().get(i)).compareToIgnoreCase(getData()) < 0)) 
					{
						i++;
					}
					getModelLangues().add(i, getData());
					
				} catch(NullPointerException p) { }
			}
		});
		
		rdbtnInterprete = new JRadioButton("Interprete");
		buttonGroup1.add(rdbtnInterprete);
		rdbtnInterprete.setBounds(200, 271, 157, 23);
		contentPane.add(rdbtnInterprete);
		rdbtnInterprete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				listLanguesParlees.setEnabled(true);
				if(!listLangueMaternelle.isSelectionEmpty()) {
					data = (String) listLangueMaternelle.getSelectedValue();
					firstClic = false;
					modelLangues.removeElement(data);
				}
			}
		});
		
	}
}
